package br.com.advance.automacao.web.utils;

import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public class BaseBean implements Serializable {

	private static final long serialVersionUID = -8371780758729335002L;

	@PostConstruct
	public final void init() {
		System.out.println("Init...");
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		ServletContext servletContext = (ServletContext) externalContext.getContext();
		WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getAutowireCapableBeanFactory().autowireBean(this);
		this.initialize();
		if (verificaUsuarioLogado()) {
			try {
				SegurancaMBean.verificaLogado(externalContext);
			} catch (IOException e) {
			}
		}
	}

	protected boolean verificaUsuarioLogado() {
		return false;
	}

	/**
	 * Toda vez que um managed bean específico precisar fazer alguma coisa no postconstruct,
	 * ele deve sobrescreve esse método, e não o init (que contém o postconstruct).
	 */
	protected void initialize() {

	}
}
